<?php
require_once 'db_connect.php';

class HerbManager {
    private $pdo;
    
    // 添加构造函数初始化数据库连接
    public function __construct() {
        $this->pdo = db_connect(); // 现在这个函数已定义
    }
    // 删除多余的 getPricePerGram() 方法
    
    // 获取所有药材
    public function getAllHerbs() {
        $sql = "SELECT * FROM herbs ORDER BY name";
        $stmt = $this->pdo->query($sql);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    
    // 根据ID获取单个药材
    public function getHerbById($id) {
        $sql = "SELECT * FROM herbs WHERE id = :id";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(['id' => $id]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
    
    // 搜索药材
    public function searchHerbs($query) {
        $sql = "SELECT * FROM herbs WHERE name LIKE :query";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(['query' => "%$query%"]);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    
    // 添加药材
    public function addHerb($data) {
        // 检查药材名称是否重复
        if ($this->checkDuplicateName($data['name'])) {
            throw new Exception('药材名称已存在，请使用不同的名称');
        }
        
        $sql = "INSERT INTO herbs (name, purchase_price, sale_price, shipping, category_price, effects, contraindications)
                VALUES (:name, :purchase_price, :sale_price, :shipping, :category_price, :effects, :contraindications)";
        
        $stmt = $this->pdo->prepare($sql);
        return $stmt->execute($data);
    }
    
    // 检查药材名称是否重复
    private function checkDuplicateName($name, $excludeId = null) {
        $sql = "SELECT COUNT(*) FROM herbs WHERE name = :name";
        $params = ['name' => $name];
        
        if ($excludeId) {
            $sql .= " AND id != :exclude_id";
            $params['exclude_id'] = $excludeId;
        }
        
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchColumn() > 0;
    }
    
    // 更新药材
    public function updateHerb($id, $data) {
        $sql = "UPDATE herbs SET 
                name = :name, 
                purchase_price = :purchase_price, 
                sale_price = :sale_price, 
                shipping = :shipping, 
                category_price = :category_price, 
                effects = :effects, 
                contraindications = :contraindications 
                WHERE id = :id";
        
        $data['id'] = $id;
        $stmt = $this->pdo->prepare($sql);
        return $stmt->execute($data);
    }
    
    // 删除药材
    public function deleteHerb($id) {
        $sql = "DELETE FROM herbs WHERE id = :id";
        $stmt = $this->pdo->prepare($sql);
        return $stmt->execute(['id' => $id]);
    }
    
    // 计算药方价格
    public function calculatePrescription($items) {
        $total = 0;
        
        foreach ($items as $item) {
            $herb = $this->getHerbByName($item['name']);
            
            if ($herb) {
                // 调用私有的getPricePerGram方法
                $pricePerGram = $this->getPricePerGram($herb);
                $total += $pricePerGram * $item['weight'];
            }
        }
        
        return $total;
    }
    
    // 获取药材每克价格
    public function getPricePerGram($herb) {
        if ($herb['sale_price']) {
            return $herb['sale_price'] / 10;
        } 
        
        if ($herb['category_price']) {
            return $herb['category_price'] / 250;
        }
        
        return ($herb['purchase_price'] * 1.2) / 10;
    }
    
    // 按名称获取药材 (保持私有)
    private function getHerbByName($name) {
        $sql = "SELECT * FROM herbs WHERE name = :name";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(['name' => $name]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
}
?>